<template>
    <div class="app-container">
        <div class="order_stat">
            <div class="title">
                <span>余额明细</span>
                <!-- <div class="com-export-dialog"><button type="button"
                        class="el-button el-button--primary el-button--small"><span>批量导出</span></button>
                </div> -->
            </div>
            <div class="stat_details">
                <div class="order_module" v-for="item, index in order_module" :key="index">
                    <div class="oirder_modul_title">
                        <div>{{ item.name }}</div>
                        <el-tooltip class="item" effect="dark" :content="item.toolip" placement="bottom">
                            <i class="el-icon-question" style="color: #afadad;margin-left: 3px;font-size: 17px;"></i>
                        </el-tooltip>
                    </div>
                    <div class="price">
                        {{ item.num }}
                    </div>
                </div>
            </div>
        </div>
        <!-- <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
            <el-tab-pane label="已结算" name="first"></el-tab-pane>
            <el-tab-pane label="未结算" name="second"></el-tab-pane>
        </el-tabs> -->
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="88px">
            <el-form-item label="会员id" prop="userId">
                <el-input v-model="queryParams.userId" placeholder="请输入会员id" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="业务类型" prop="businessType">
                <el-select clearable v-model="queryParams.businessType" placeholder="请选择业务类型" style="width:100%">
                    <el-option v-for="item in queryBusinessType" :key="item" :value="item">{{ item
                        }}</el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="创建时间">
                <el-date-picker v-model="selectTime" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                    range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                    @change="selectTimeChange"></el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <!-- <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['system:good:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['system:good:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                    @click="handleDelete" v-hasPermi="['system:good:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['system:good:export']">批量导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row> -->

        <el-table v-loading="loading" :data="goodList" @selection-change="handleSelectionChange">
            <!-- <el-table-column type="selection" width="55" align="center" /> -->
            <el-table-column label="会员ID" align="center" prop="userId" width="120" />
            <el-table-column label="会员" align="center" prop="nickName" />
            <el-table-column label="业务类型" align="center" prop="businessType" />
            <el-table-column label="收入/支出" align="center" prop="classifyImg">
                <template slot-scope='{row}'>
                    {{ `${row.addOrSubtract == 1 ? '+' : '-'}${row.variation}` }}
                </template>
            </el-table-column>
            <el-table-column label="变动前余额" align="center" prop="variationBefore">
            </el-table-column>
            <el-table-column label="变动后余额" align="center" prop="variationAfter">
            </el-table-column>
            <el-table-column label="创建时间" align="center" prop="createTime">
            </el-table-column>
            <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['system:good:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['system:good:remove']">删除</el-button>
                </template>
            </el-table-column> -->
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改供应商商品对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="分类名称" prop="deptName">
                    <el-input v-model="form.deptName" placeholder="请输入分类名称" />
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input v-model="form.sort" placeholder="请输入排序" />
                </el-form-item>
                <el-form-item label="分类图标" prop="classifyImg">
                    <image-upload @input="uploadImg" :value="form.classifyImg" :limit="1" />
                </el-form-item>
                <el-form-item label="是否启用" prop="status">
                    <el-switch v-model="form.status">
                    </el-switch>
                </el-form-item>
                <el-form-item label="是否显示" prop="goodId">
                    <el-switch v-model="form.delFlag">
                    </el-switch>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { goodClassifyList, goodClassifyDetail, goodclassifyAdd, goodclassifyEdit, goodclassifyDelete } from "@/api/system/goods/classify";
import { listPage, statisticalBalance } from "@/api/assets/balance";
import DictData from "../../../components/DictData";

export default {
    name: "Good",
    data() {
        return {
            order_module: [
                { name: '累计余额', num: 1939376.64, toolip: '累计所有平台产生的余额' },
                { name: '已使用', num: 1939376.64, toolip: '使用余额支付或使用余额抵扣统计' },
                { name: '会员账户余额', num: 1939376.64, toolip: '所有会员现有账户的余额' },
                { name: '已提现余额', num: 1939376.64, toolip: '已提现的账户累计余额' },
            ],
            checkboxList: '',
            activeName: 'first',
            // 日期范围
            dateRange: [],
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 供应商商品表格数据
            goodList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                variationType: 1,
                userId: null,
                businessType: null,
                createTime: null,
                startTime: null,
                endTime: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                productLevel: [
                    { required: true, message: "商品等级 1vip 3共享店主 4联创 5区域运营 6总裁不能为空", trigger: "blur" }
                ],
            },
            selectTime: [],
            queryBusinessType: []
        };
    },
    async created() {
        if (this.$route.query) {
            this.queryParams.userId = this.$route.query.userId
        }
        this.getList();
        this.getCountBalance()
        this.queryBusinessType = await this.DictData.queryBusinessType()
    },
    methods: {
        handleClick(tab, event) {
            console.log(tab, event);
            this.getList()
        },
        /** 查询供应商商品列表 */
        getList() {
            this.loading = true;
            // this.addDateRange(this.queryParams, this.dateRange)
            listPage(this.queryParams).then(response => {
                this.goodList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        /**统计余额 */
        getCountBalance() {
            statisticalBalance().then(res => {
                this.order_module.forEach(item => {
                    switch (item.name) {
                        case '累计余额':
                            item.num = res.data.cumulativeBalances
                            break;
                        case '已使用':
                            item.num = res.data.useBalance
                            break;
                        case '会员账户余额':
                            item.num = res.data.memberAccountBalance
                            break;
                        case '已提现余额':
                            item.num = res.data.cashWithdrawalBalance
                            break;
                    }
                })
            })
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                deptName: null,
                sort: null,
                classifyImg: null,
                status: null,
                delFlag: null,
            };
            this.dateRange = [];
            this.resetForm("form");
        },
        uploadImg(v) {
            this.form.classifyImg = v
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.selectTime = []
            this.queryParams.startTime = null
            this.queryParams.endTime = null
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加商品分类";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            goodClassifyDetail(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改商品分类";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    this.form.status = this.form.status ? '0' : '1'
                    this.form.delFlag = this.form.delFlag ? '0' : '1'
                    if (this.form.id != null) {
                        goodclassifyEdit(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        goodclassifyAdd(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除数据？').then(function () {
                return goodclassifyDelete(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('system/good/export', {
                ...this.queryParams
            }, `good_${new Date().getTime()}.xlsx`)
        },
        selectTimeChange(v) {
            this.queryParams.startTime = v[0]
            this.queryParams.endTime = v[1]
        },
    }
};
</script>
<style lang="less" scoped>
.app-container {
    width: 100%;
    height: 1005;
    background-color: #fff;

    .order_stat {
        background: #FFF;
        padding: 10px 10px 0;
        margin-bottom: 10px;

        .title {
            font-size: 18px;
            font-weight: bold;
            display: flex;
            align-items: center;
            height: 40px;
            display: flex;
            justify-content: space-between;
        }

        .stat_details {
            padding: 30px 0;
            display: flex;

            .order_module {
                flex: 1;
                background: #F2F2F2;
                margin: 0 5px;
                padding: 20px 25px;

                .oirder_modul_title {
                    font-size: 12px;
                    margin-bottom: 10px;
                    display: flex;
                    align-items: center;
                    color: #46a6ff;
                }

                .price {
                    font-size: 22px;
                    font-weight: bold;
                }
            }
        }

    }
}
</style>